package org.herui.campus.service.properties;

import org.herui.campus.properties.PropertyValueInfo;
import org.shan.spring.cache.CacheChannel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author zzb
 * @version 1.0
 * @date 2024/2/1 15:01
 */
@Service
public class ManagerProperties {

    @Autowired
    protected CacheChannel cacheChannel;
    private List<BaseProperties> baseProperties = new LinkedList<>();
    @Autowired(required = false)
    private SysProperties sysProperties;
    @Autowired(required = false)
    private MakeWinProperties makeWinProperties;
    @Autowired(required = false)
    private WushuiProperties wushuiProperties;
    @Autowired(required = false)
    private EnergyProperties energyProperties;
    @Autowired(required = false)
    private SceneProperties sceneProperties;

    @Autowired(required = false)
    private JlwProperties jlwProperties;

    @PostConstruct
    public void init() {
        if (sysProperties != null) {
            sysProperties.setCacheChannel(cacheChannel);
            sysProperties.init();
            baseProperties.add(sysProperties);
        }

        if (makeWinProperties != null) {
            makeWinProperties.setCacheChannel(cacheChannel);
            makeWinProperties.init();
            baseProperties.add(makeWinProperties);
        }

        if (wushuiProperties != null) {
            wushuiProperties.setCacheChannel(cacheChannel);
            wushuiProperties.init();
            baseProperties.add(wushuiProperties);
        }

        if (energyProperties != null) {
            energyProperties.setCacheChannel(cacheChannel);
            energyProperties.init();
            baseProperties.add(energyProperties);
        }

        if (sceneProperties != null) {
            sceneProperties.setCacheChannel(cacheChannel);
            sceneProperties.init();
            baseProperties.add(sceneProperties);
        }

        if (jlwProperties != null) {
            jlwProperties.setCacheChannel(cacheChannel);
            jlwProperties.init();
            baseProperties.add(jlwProperties);
        }
    }

    public List<BaseProperties> getBaseProperties() {
        return baseProperties;
    }

    public SysProperties getSysProperties() {
        return sysProperties;
    }

    public MakeWinProperties getMakeWinProperties() {
        return makeWinProperties;
    }

    public WushuiProperties getWushuiProperties() {
        return wushuiProperties;
    }

    public EnergyProperties getEnergyProperties() {
        return energyProperties;
    }

    public SceneProperties getSceneProperties() {
        return sceneProperties;
    }

    public JlwProperties getJlwProperties() {
        return jlwProperties;
    }

    public void set(String region, PropertyValueInfo propertyValueInfo) {

        for (BaseProperties basePropertie : baseProperties) {
            Map<String, List<PropertyValueInfo>> map = basePropertie.getMap();
            if (map.containsKey(region)) {
                basePropertie.set(region, propertyValueInfo);
                return;
            }
        }
    }

    public PropertyValueInfo get(String region, String key) {
        for (BaseProperties basePropertie : baseProperties) {
            Map<String, List<PropertyValueInfo>> map = basePropertie.getMap();
            if (map.containsKey(region)) {
                List<PropertyValueInfo> valueInfos = map.get(region);
                if (valueInfos != null) {
                    for (PropertyValueInfo valueInfo : valueInfos) {
                        if (valueInfo.getKey().equals(key)) {
                            return valueInfo;
                        }
                    }
                }
            }
        }
        return null;
    }
}
